import axios from 'axios';
import qs from 'qs'

// 添加错误的轻提示
import { Toast } from 'vant';

//网址未修改
const instance = axios.create({
    baseURL: 'http://120.79.253.131:3000',
    timeout: 10000,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
});




instance.interceptors.response.use(function (response) {
   
    return response.data;
  }, function (error) {

    return Promise.reject(error);
  });

//axios解决的是ajax请求的问题，promise解决回调地狱的问题
const http = {
    //get请求
    get(url, params) {
        return new Promise((resolve )=> {
            instance.get(url,{
                params: params
              })
                .then((res) => {
                    if(res.status=='0'){
                        resolve(res)
                    }
                })
                .catch((err) => {
                    Toast(err.message)
                })
        })
    },
    //post请求
    post(url, params) {
        return new Promise((resolve) => {
            instance.post(url,  qs.stringify(params))
                .then((res) => {
                    if (res.status === '0') {
                        resolve(res)
                    } else {
                        Toast(res.msg)
                    }
                })
                .catch((err) => {
                    Toast(err.message)
                })
        })
    }
}
export default http